WooCommerce: prevent image duplication when sideloading via the REST API#1165
Merged
gabrielcld2 merged 3 commits intodevelopfrom Apr 27, 2026
Merged
Conversation
PatelUtkarsh
approved these changes
Apr 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a product is created via the WooCommerce REST API and the
imagespayload contains a Cloudinary URL, WooCommerce unconditionally sideloads the image into the WordPress media library as a new attachment. The Cloudinary plugin then auto-syncs this new attachment back up to Cloudinary, resulting in a duplicate asset on the Cloudinary account.Approach
The fix intercepts the REST request before WooCommerce processes images, using the
rest_request_before_callbacksfilter — the same filter already used by the Cloudinary plugin'sDeliveryclass.For any image in the request that has a
srcbut noid, we check whether the URL is a Cloudinary URL. If it is, we attempt to resolve it to an existing media library attachment using a three-step lookup:the library.
get_linked_attachments(). Covers the most common case: an image uploaded from WordPress toCloudinary with no transformations applied.
base_<public_id>meta key written for any asset imported directly from the Cloudinary dashboard.When a match is found, the
idfield is populated on the image entry before the request reachesWC_REST_Products_Controller::set_product_images().WooCommerce's own guard (
if ( 0 === $attachment_id )) then skips the sideload entirely. If no match is found across all three steps, the request falls through unchanged and WooCommerce sideloads normally — existing behaviour is fully preserved.The hook is registered in a new
setup_rest_hooks()method onWooCommerceGallery, called whenever WooCommerce is active, independently of the Cloudinary gallery replacement toggle.QA Instructions
Prerequisites
Generate a WooCommerce REST API key
QA Test), set permissions to Read/Write, and click Generate API key.Seed a Cloudinary image in the media library
The fix only prevents duplication when the image already exists in the WordPress media library as a Cloudinary-synced asset. Upload or import a Cloudinary asset into the media library via the Cloudinary plugin's media library sync so that it has the relevant post meta (
_public_id, sync key) stored.Take note of the full Cloudinary URL for that asset (e.g. from the media library's Cloudinary URL field, or from the Cloudinary console).
Test
Run the following curl request, substituting your own credentials, local domain, and Cloudinary image URL:
After the request: